package com.woniuxy.micromallserver.ordermag.controller;

import com.woniuxy.common.bean.PageDto;
import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.micromallserver.bean.OrderBean;
import com.woniuxy.micromallserver.ordermag.service.IOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author unreleased
 * @date 2021/1/25 17:11
 * 订单请求
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private IOrderService orderServiceImpl;

    @GetMapping(value = "/getOrderList")
    public ResultMsg getOrderList(OrderBean orderBean,Integer pageNum,Integer pageSize){
        ResultMsg resultMsg = new ResultMsg();
        PageDto pageDto = orderServiceImpl.findOrderListByParams(orderBean, pageNum, pageSize);
        if (pageDto == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(pageDto);
    }

    /**
     * 发货请求
     * @param deliveryId 快递单号
     * @param id 订单id
     * @param receiveTel 收货人电话
     * @return 响应
     */
    @PutMapping("/updateDelivery")
    public ResultMsg updateDelivery(String deliveryId, Long id,String receiveTel){
        ResultMsg resultMsg = new ResultMsg();
        if(orderServiceImpl.updateDelivery(deliveryId,id,receiveTel)){
            //rabbitMQ进行短信进行物流通知
            return resultMsg.setCode(200).setMessage("发货成功");
        }else{
            return resultMsg.setCode(10000).setMessage("后台繁忙");
        }
    }
    @PutMapping("/close")
    public ResultMsg close (OrderBean orderBean){
        ResultMsg resultMsg = new ResultMsg();
        if(orderServiceImpl.updateOrder(orderBean)){
            return resultMsg.setCode(200).setMessage("成功关闭订单");
        }else{
            return resultMsg.setCode(10000).setMessage("后台繁忙");
        }
    }
    @DeleteMapping("/closeAll")
    public ResultMsg closeAll(String [] arr){
        //[0=1&1=18]
        ResultMsg resultMsg = new ResultMsg();
        String[] arr1 = arr[0].split("&");
        int[] ids = new int[arr1.length];
        for(int i=0;i<arr1.length;i++){
            String[] arr2 = arr1[i].split("=");
            int id = Integer.parseInt(arr2[1]);
            ids[i] = id;
        }
        System.out.println(Arrays.toString(ids));
        //调用service层关闭订单
        if(orderServiceImpl.closeAllOrders(ids)){
            return resultMsg.setCode(200).setMessage("操作成功");
        }else {
            return resultMsg.setCode(10000).setMessage("操作失败");
        }
    }
}
